var appIndex = angular.module('app-index', []);
appIndex.controller('ctl-index', function($scope, $http) {
	$scope.logFail = true;
	$scope.loginSubmit = function() {
		$http({
		    method : 'GET',
		    url : "/user/" + $scope.username + "/" + $scope.password
		}).then(function successCallback(response) {
			if (response.data == 'success') {
				window.location.href = "index.html";
			} else {
				$scope.logFail = false;
				$scope.logFailMsg = response.data;
			}

		}, function errorCallback(response) {
			alert(response.data);
		});

	};

	$scope.clikLogout = function() {
		$http({
		    method : 'DELETE',
		    url : "/user/session"
		}).then(function successCallback(response) {
			$scope.unLogin = false;
			$scope.loginIn = true;
			$scope.register = false;
			$scope.logout = true;
		});

	};

	$http({
	    method : 'GET',
	    url : "/user/session"
	}).then(function successCallback(response) {
		$scope.sessionName = response.data;
		if (response.data != "null" && response.data != "") {
			$scope.unLogin = true;
			$scope.loginIn = false;
			$scope.register = true;
			$scope.logout = false;
		} else {
			$scope.unLogin = false;
			$scope.loginIn = true;
			$scope.register = false;
			$scope.logout = true;
		}

	}, function errorCallback(response) {
	});

	$http({
	    method : 'GET',
	    url : "/book/latest"
	}).then(function successCallback(response) {
		$scope.booksLatest = response.data;
	}, function errorCallback(response) {
	});

	$http({
	    method : 'GET',
	    url : "/book/best-selling"
	}).then(function successCallback(response) {
		$scope.booksBestSelling = response.data;
	}, function errorCallback(response) {
	});

	$http({
	    method : 'GET',
	    url : "/book/bargain"
	}).then(function successCallback(response) {
		$scope.booksBargain = response.data;
	}, function errorCallback(response) {
	});

	$http({
	    method : 'GET',
	    url : "/book/recommended"
	}).then(function successCallback(response) {
		$scope.booksRecommended = response.data;
	}, function errorCallback(response) {
	});
});
